package com.hl.card.mq;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.mq.Mq4Base;
import com.hl.card.modular.pack.dao.DisMemberApplyMapper;
import com.rabbitmq.client.Channel;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class Mq4NotifyDemo extends Mq4Base {
    String logPrefix = "【延时消息测试】";
    @Autowired
    private static int count = 0;
	@Resource(name = "disMemberApplyYMapper")
	private DisMemberApplyMapper disMemberApplyMapper;
	
    public void send(String queue_name, String msg) {
        super.send(queue_name, msg);
    }

    public void send(String queue_name, String msg, long delay) {
        super.send(queue_name, msg, delay);
    }
    
    /**
     * 监听奖励
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues= MqCustomConfig.QUEUE_AWARD_API)
    public void receiveCallback(Message message, Channel channel) throws IOException {
    	String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
    	if(msg.indexOf("\"")==0)
    	{
    	  msg=msg.substring(1,msg.length()-1);	
    	}
      	System.out.println(msg);
    	JSONObject obj=JSONObject.parseObject(msg);
    	JSONObject data=obj.getJSONObject("data");
    	String orderNo=data.getString("mchOrderNo");
    	Map<String,Object> param=new HashMap<String,Object>();
    	param.put("order_no", orderNo);
    	param.put("reward_remark", msg);
    	disMemberApplyMapper.updateRewardInfo(param);
  
    }
    
    /**
     * 监听状态
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues= MqCustomConfig.QUEUE_DELAY_STATUS)
    public void statusCallback(Message message, Channel channel) throws IOException {
    	String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
    	if(msg.indexOf("\"")==0)
    	{
    	  msg=msg.substring(1,msg.length()-1);	
    	}
    	JSONObject obj=JSONObject.parseObject(msg);
    	String order_no=obj.getString("orderNo");
        System.out.println(msg);
    	disMemberApplyMapper.cancelDisMemberApplyByNo(order_no);
    
  
    }

}